package com.example.databinding.viewModel;

import android.util.Log;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.example.databinding.BR;
import com.example.databinding.bean.User;

//继承BaseObservable实现双向绑定
public class UserViewModel extends BaseObservable {
    private User user;

    public UserViewModel() {
        this.user = new User("Jack");
    }

    //当userName改变时会调用此方法改变xml中的数据
    @Bindable
    public String getUserName() {
        return user.userName;
    }

    //当xml的数据改变时会调用此方法 更新user中的userName的值
    public void setUserName(String userName) {
        Log.d("guaimiao", "set userName " + userName);
        if (userName != null && !userName.equals(user.userName)) {
            user.userName = userName;
            notifyPropertyChanged(BR.userName);
        }
    }
}
